//
//  LYRadioHelper.m
//  LY
//
//  Created by lanou3g on 15/12/15.
//  Copyright © 2015年 梁媛. All rights reserved.
//

#import "LYRadioHelper.h"
static LYRadioHelper *helper = nil;

@interface LYRadioHelper ()
@property(nonatomic,strong)AVPlayer *RadioPlayer;
@property(nonatomic,assign)BOOL shifoubofang;
@property(nonatomic,strong)NSTimer *sliderTimer;
@end

@implementation LYRadioHelper

- (BOOL)isPlaying{
    return self.shifoubofang;
}
//单例
+ (instancetype)shareLYRadioHelper{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!helper) {
            helper = [[LYRadioHelper alloc] init];
            [[NSNotificationCenter defaultCenter] addObserver:helper selector:@selector(endRadioAction) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
        }
    });
    return helper;
}
//播放结束
- (void)endRadioAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(RadioPlayEndWith:)]) {
        [self.delegate RadioPlayEndWith:self];
    }
}

//播放、暂停
- (void)playOrPause{
    if (self.isPlaying) {
        [self pauseRadio];
    }else{
        [self playRadio];
    }
}
//播放
- (void)playRadio{
    [self.RadioPlayer play];
    self.shifoubofang = YES;
    if (self.sliderTimer) {
        return;
    }
    self.sliderTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}
- (void)timerAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(playerTime:LYRadiohelper:)]) {
        float time = self.RadioPlayer.currentTime.value / self.RadioPlayer.currentTime.timescale;
        [self.delegate playerTime:time LYRadiohelper:self];
    }
}
//暂停
- (void)pauseRadio{
    [self.RadioPlayer pause];
    self.shifoubofang = NO;
    [self.sliderTimer invalidate];
    self.sliderTimer = nil;
}
//播放（给URL播放）
- (void)playStringWithUrl:(NSString *)url{
    if (!url && ![url isKindOfClass:[NSString class]]) {
        return;
    }
    if (self.RadioPlayer.currentItem) {
        [self.RadioPlayer.currentItem removeObserver:self forKeyPath:@"status"];
    }
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
    [item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    [self.RadioPlayer replaceCurrentItemWithPlayerItem:item];
}
//实现观察者方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if (![keyPath isEqualToString:@"status"]) {
        return;
    }
    AVPlayerItemStatus status = [change[@"new"] integerValue];
    switch (status) {
        case AVPlayerItemStatusUnknown:
            
            break;
        case AVPlayerItemStatusFailed:
            
            break;
        case AVPlayerItemStatusReadyToPlay:
            [self playRadio];
            break;
        default:
            break;
    }
}

//从某个时刻开始播放
- (void)seekTimePlayWithTime:(float)time{
    if (self.isPlaying) {
        [self pauseRadio];
    }
    [self.RadioPlayer seekToTime:CMTimeMakeWithSeconds(time, self.RadioPlayer.currentTime.timescale) completionHandler:^(BOOL finished) {
        [self playRadio];
    }];
}

- (AVPlayer *)RadioPlayer{
    if (!_RadioPlayer) {
        _RadioPlayer = [[AVPlayer alloc] init];
    }
    return _RadioPlayer;
}

//移除通知
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:AVPlayerItemDidPlayToEndTimeNotification];
}

@end
